package me.wsj.fengyun.utils;

import android.view.View;

/**
 * 点击事件工具类
 * Created by cywu4 on 2018/1/4.
 */

public class ClickUtils {

    private static final int CLICK_INTERVAL_MS = 300;

    private static long sLastClickTimeMs;

    private static long sLastView;

    /**
     * 判断用户是否是快速点击
     *
     * @return 是否是快速点击
     */
    public static boolean isFastClick() {
        long curTime = System.currentTimeMillis();
        if (curTime - sLastClickTimeMs > CLICK_INTERVAL_MS || curTime < sLastClickTimeMs) {
            sLastClickTimeMs = curTime;
            //不是快速点击
            return false;
        }
        return true;
    }

    /**
     * 判断用户是否是快速点击
     *
     * @return 是否是快速点击
     */
    public static boolean isFastClick(long interval) {
        long curTime = System.currentTimeMillis();
        if (curTime - sLastClickTimeMs > interval || curTime < sLastClickTimeMs) {
            sLastClickTimeMs = curTime;
            //不是快速点击
            return false;
        }
        return true;
    }

    /**
     * 判断用户是否是快速点击
     *
     * @return 是否是快速点击
     */
    public static boolean isFastClick(View view, long interval) {
        long hashCode = 0;
        if (view != null) {
            hashCode = view.hashCode();
        }
        //view相等，并且是快速点击
        if (hashCode == sLastView && isFastClick(interval)) {
            sLastView = hashCode;
            return true;
        }
        //不是快速点击
        sLastView = hashCode;
        return false;
    }

}
